//
//  SinaWeiboManager.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-2-22.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "SinaWeiboManager.h"

@implementation SinaWeiboManager

static SinaWeiboManager *sinaWeiboManager;

+ (id)defaultManager
{
    if(sinaWeiboManager == nil){
        sinaWeiboManager = [[SinaWeiboManager alloc] init];
    }
    return sinaWeiboManager;
}

- (id)init
{
    self = [super init];
    if(self){
        _sinaweibo = [[SinaWeibo alloc] initWithAppKey:kAppKey appSecret:kAppSecret appRedirectURI:kAppRedirectURI andDelegate:self];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSDictionary *sinaweiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"];
        if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfo objectForKey:@"ExpirationDateKey"] && [sinaweiboInfo objectForKey:@"UserIDKey"])
        {
            _sinaweibo.accessToken = [sinaweiboInfo objectForKey:@"AccessTokenKey"];
            _sinaweibo.expirationDate = [sinaweiboInfo objectForKey:@"ExpirationDateKey"];
            _sinaweibo.userID = [sinaweiboInfo objectForKey:@"UserIDKey"];
        }
    }
    return self;
}

- (void)removeAuthData
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SinaWeiboAuthData"];
}

- (void)storeAuthData
{
    SinaWeibo *sinaweibo = [self sinaweibo];
    
    NSDictionary *authData = [NSDictionary dictionaryWithObjectsAndKeys:
                              sinaweibo.accessToken, @"AccessTokenKey",
                              sinaweibo.expirationDate, @"ExpirationDateKey",
                              sinaweibo.userID, @"UserIDKey",
                              sinaweibo.refreshToken, @"refresh_token", nil];
    [[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)logStateDidChange
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kSinaWeiboLogStateDidChange object:nil];
}

#pragma mark - SinaWeibo Delegate

- (void)sinaweiboDidLogIn:(SinaWeibo *)sinaweibo
{
    NSLog(@"sinaweiboDidLogIn userID = %@ accesstoken = %@ expirationDate = %@ refresh_token = %@", sinaweibo.userID, sinaweibo.accessToken, sinaweibo.expirationDate,sinaweibo.refreshToken);
    
    [self logStateDidChange];
    [self storeAuthData];
    
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"绑定成功"] autorelease];
    [promptView show];
}

- (void)sinaweiboDidLogOut:(SinaWeibo *)sinaweibo
{
    NSLog(@"sinaweiboDidLogOut");
    [self removeAuthData];
    [self logStateDidChange];
}

- (void)sinaweiboLogInDidCancel:(SinaWeibo *)sinaweibo
{
    NSLog(@"sinaweiboLogInDidCancel");
    [self removeAuthData];
    [self logStateDidChange];
}

- (void)sinaweibo:(SinaWeibo *)sinaweibo logInDidFailWithError:(NSError *)error
{
    NSLog(@"sinaweibo logInDidFailWithError %@", error);
    [self removeAuthData];
    [self logStateDidChange];
}

- (void)sinaweibo:(SinaWeibo *)sinaweibo accessTokenInvalidOrExpired:(NSError *)error
{
    NSLog(@"sinaweiboAccessTokenInvalidOrExpired %@", error);
    [self removeAuthData];
    [self logStateDidChange];
    UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"新浪微博授权已过期" message:@"您需要重新绑定" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"绑定", nil] autorelease];
    alertView.tag = 1;
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1) {
        if (buttonIndex == 1) {
            [self.sinaweibo logIn];
        }
    }
}

@end
